<?php
include_once 'config/config.php';

$action = isset($_GET['action']) ? $_GET['action'] : '';

if( $action == 'index_blog') {
	$limit = isset($_GET['limit']) ? $_GET['limit'] : '';
	$r = getSql("select * from {$dbpre}article limit {$limit}" );
	echo echo_json(0, $r);die;
}

if( $action == 'about_blog') {
	$limit = isset($_GET['limit']) ? $_GET['limit'] : '';
	$r = getSql("select * from {$dbpre}article where art_cate_id = 2 limit {$limit}" );
	echo echo_json(0, $r);die;
}

if( $action == 'blog_blog') {
	$limit = isset($_GET['limit']) ? $_GET['limit'] : '';
	$r = getSql("select * from {$dbpre}article where art_cate_id = 1 limit {$limit}" );
	echo echo_json(0, $r);die;
}

if( $action == 'index_setting') {
	$r = getAll('setting');
	echo echo_json(0, $r);die;
}

// if( $action == 'common_setting') {
// 	$r = getAll('setting');
// 	echo echo_json(0, $r);die;
// }

if( $action == 'common_nav') {
	$limit = isset($_GET['limit']) ? $_GET['limit'] : 4;
	$r = getSql("select * from {$dbpre}nav order by nav_order limit {$limit}");
	echo echo_json(0, $r);die;
}

if( $action == 'common_setting') {
	$logo = getOne("setting", "set_name='logo'");
	$introl = getOne("setting", "set_name='introduction'");
	$copyright = getOne("setting", "set_name='copyright'");

	$r = array(
		"logo" => $logo,
		"introl" => $introl,
		"copyright" => $copyright
	);

	echo echo_json(0, $r);die;
}

//查询一篇博客
if( $action == 'one_blog') {
	$id = isset($_GET['id']) ? $_GET['id'] : '';
	$r = getSql("select * from {$dbpre}article where art_id = ${id} limit 1");
	echo echo_json(0, $r);die;
}

//查询留言
if( $action == 'message_show') {
	$limit = isset($_GET['limit']) ? $_GET['limit'] : '';
	$r = getSql("select * from {$dbpre}message order by create_time desc limit ${limit}");
	foreach ($r as $key => $value) {
		$r[$key]['create_time']=date("Y-m-d H:i:s",$value['create_time']);
	}
	echo echo_json(0, $r);die;
}

//接收表单并插入数据库
if($_POST){
	if(isset($_POST['data']) && $_POST['data']){
		$data = $_POST['data'];
		$data['msg_is_show'] = 1;
		$data['create_time'] = time();
		// var_dump($data);
		$r = insert("message",$data);
		if($r) {
			echo echo_json(0, $r, '插入成功');die;
		}else{
			echo echo_json(0, '', '插入失败');die;
		}
	}
}